package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.ResponseResult;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Levi
 * @since 2022-09-29
 */
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public ResponseResult<List<IntegralGrade>> list(){
//        log.info("info....");
//        log.warn("warn....");
//        log.error("error....");
        List<IntegralGrade> list = integralGradeService.list();
        return ResponseResult.result(ResponseEnum.SUCCESS,list);
        //return ResponseResult.success(list);
    }

    @ApiOperation(value = "根据id删除积分记录",notes = "逻辑删除积分记录")
    @DeleteMapping("/removeById/{id}")
    public ResponseResult removeById(@ApiParam(value = "数据id" ,example = "2",defaultValue = "1",required = true) @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return ResponseResult.success("删除成功");
        }else{
            return ResponseResult.fail("删除失败");
        }
    }

    @ApiOperation("新增积分记录信息")
    @PostMapping("/save")
    public ResponseResult save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
//        if(integralGrade.getBorrowAmount()==null){
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean b = integralGradeService.save (integralGrade);
        if(b){
            return ResponseResult.success("保存成功");
       }else{
            return ResponseResult.fail("保存失败");
        }
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/getById/{id}")
    public ResponseResult<IntegralGrade> getById(@ApiParam(value = "数据id",required = true) @PathVariable Long id){
        IntegralGrade byId = integralGradeService.getById(id);
        if(byId==null){
            return ResponseResult.fail("获取失败");
        }else{
            return ResponseResult.success("获取成功",byId);
        }
    }

    @ApiOperation("根据id更新积分等级")
    @PutMapping("/update")
    public ResponseResult<IntegralGrade> update(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        if(!b){
             return ResponseResult.fail("更新失败");
        }else{
            return ResponseResult.success("更新成功");
        }
    }
}

